package com.panshi.modules.controller;

import com.panshi.api.SearchApi;
import com.panshi.po.Article;
import com.panshi.modules.service.ISearchService;

import com.panshi.po.Search;
import com.panshi.vo.Result;
import com.panshi.vo.SearchPageVO;
import com.panshi.vo.SearchQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("search-api")
public class SearchController implements SearchApi{

    @Autowired
    private ISearchService searchService;

    /**
     * 搜索
     * @param searchQueryVO
     * @return
     */
    public Result<Set<Search>> search(SearchQueryVO searchQueryVO){
        Result<Set<Search>> setResult = new Result<>();
        Set<Search> fenye = searchService.fenye(searchQueryVO);
        setResult.ok().setData(fenye);
        return setResult;
    }

    /**
     * 分页
     * @return
     */
    public Result<SearchPageVO> paging(SearchQueryVO searchQueryVO){
        Result<SearchPageVO> result = new Result<>();
        SearchPageVO searchPageVO = new SearchPageVO();
        searchQueryVO.setCurrent(searchQueryVO.getCurrent()==null ? 1 : searchQueryVO.getCurrent());
        Integer total = searchService.toTotal(searchQueryVO);
        searchPageVO.setTotal(total);
        searchPageVO.setCurrent(searchQueryVO.getCurrent());
        result.ok().setData(searchPageVO);
        return result;
    }

    /**
     * 热搜
     * @return
     */
    public Result<Set<Article>> hotSearch(){
        Result<Set<Article>> result = new Result<Set<Article>>();
        Set<Article> hotSearch = searchService.findSou();
        result.ok().setData(hotSearch);
        return result;
    }
}
